<?php
namespace app\admin\Controller;

use app\admin\model\User;
use think\Controller;
use think\Request;
use think\Config;
use think\Session;
use think\View;
use app\admin\model\LoginModel;

class Login extends Controller
{   

    private $loginModel;
    public function _initialize()
    {
        parent::_initialize();
        $this->loginModel = new LoginModel(); 
    }

    public function login(){
        $this->assign("title","欣五加");
        $this->assign("tel","10086");
        return $this->fetch("login");
    }

    public function login1(){
        return $this->fetch("login1");
    }

    public function index(){
       
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        $user = $this->loginModel->mobileCheck($account);
        if($user == false){
            $this->error('账号不存在');
        }

        $entry = $this->loginModel->entry($account,$password);
        if($entry == false){
            $this->error('密码错误请输入正确密码！');
        }
		$arr = $this->loginModel->alls($entry['roleid']);
        foreach($arr as $v){
            $list[$v['menuid']] = $v['url'];
        }
        Session::set('allst',$list);
        Session::set('user',$entry['uid']);
        $this->redirect('Index/index');       
    }

    public function testIndex(){
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        $code= $this->request->post('code');

        if(!(new \think\captcha\Captcha())->check($code)){
            $this->error("验证码错误");
        }

        $user=new User();
        $data=$user->where([
            "mobile"        =>$account,
            "password"      =>$password
        ])->find();

        if(!$data){
            $this->error("用户名或密码错误");
        }

        (new Session())->set("user",[
            "id"        =>$data->uid,
            "scope"     =>$data->scope,
            "nickname"  =>$data->nickname
        ]);
        $this->redirect("index/index");

    }
}
